#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#if defined(TEST_TARGET_unitt)
#include <varch/command.h>
#include <varch/unitt.h>
#include <varch/unitt.h>
#else  
#include "init.h"
#include "command.h"
#include "unitt.h"
#include "kern.h"
#include "unitt.h"
#include "kern.h"
#endif

/************************************************************************************/
/************************************* Unit Test ************************************/
/************************************************************************************/

// #define EXIT_TEST
extern uint64_t unitt_clock(void);

static int test_0(void)
{
    for (int i = 0; i < 100; i++)
    {
        if (0) 
        {
            
            #if defined (EXIT_TEST)
            exit(0);
            #endif 
            return UNITT_E_FAIL;
        }
    }
    
    return UNITT_E_OK;
}

static void unitt_task(void)
{
    static UNITT_TCASE rand_tests[] = {
        UNITT_TCASE(test_0),
        // UNITT_TCASE(test_1),
        // UNITT_TCASE(test_2),
    };

    static UNITT suites[] = {
        { "xxx suite", rand_tests, sizeof(rand_tests) / sizeof(rand_tests[0]) , unitt_clock },
    };

    UNITT_EXE(suites);
}

/************************************************************************************/
/************************************* Base Test ************************************/
/************************************************************************************/

#if 0
#ifdef _WIN32
#include <windows.h>
#include <psapi.h> // -lPsapi
#else
#include <unistd.h>
#include <pthread.h>
#endif

void get_memory_usage(int* resident_set_size, int* virtual_memory_size) 
{
#ifdef _WIN32
    PROCESS_MEMORY_COUNTERS pmc;
    if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) {
        *resident_set_size = pmc.WorkingSetSize / 1024; // Converting to KB
        *virtual_memory_size = pmc.PagefileUsage / 1024; // Converting to KB
    } else {
        *resident_set_size = 0;
        *virtual_memory_size = 0;
    }
#else
    FILE* file = fopen("/proc/self/statm", "r");
    if (file) {
        fscanf(file, "%d %d", resident_set_size, virtual_memory_size);
        fclose(file);
    } else {
        *resident_set_size = 0;
        *virtual_memory_size = 0;
    }
#endif
}
#endif 

#define SINGLE_TCOUNT 1000

extern uint64_t unitt_clock(void);

int unitt_add(int a, int b) 
{
    return a + b;
}

int unitt_subtract(int a, int b) 
{
    return a - b;
}

int unitt_multiply(int a, int b) 
{
    return a * b;
}

float unitt_divide(int a, int b) 
{
    if (b == 0) 
    {
        return 0;
    }
    return (float)a / (float)b;
}

int setup() 
{
    return 0;
}

int teardown() 
{
    return 0;
}

int random_input() 
{
    return rand() % 100;
}

int test_add() 
{
    for (int i = 0; i < SINGLE_TCOUNT; i++) 
    { 
        int a = random_input();
        int b = random_input();
        if (UNITT_E_FAIL == unitt_det_equal(unitt_add(a, b), a + b, "Random addition failed")) return UNITT_E_FAIL;
    }

    return UNITT_E_OK;
}

int test_subtract() 
{
    for (int i = 0; i < SINGLE_TCOUNT; i++) 
    { 
        int a = random_input();
        int b = random_input();
        if (UNITT_E_FAIL == unitt_det_equal(unitt_subtract(a, b), a - b, "Random subtraction failed")) return UNITT_E_FAIL;
    }

    return UNITT_E_OK;
}

int test_multiply() 
{
    for (int i = 0; i < SINGLE_TCOUNT; i++) 
    { 
        int a = random_input();
        int b = random_input();
        if (UNITT_E_FAIL == unitt_det_equal(unitt_multiply(a, b), a * b, "Random multiplication failed")) return UNITT_E_FAIL;
    }

    return UNITT_E_OK;
}

int test_divide() 
{
    for (int i = 0; i < SINGLE_TCOUNT; i++) 
    { 
        int a = random_input();
        int b = random_input();
        if (b != 0) 
        {
            if (UNITT_E_FAIL == unitt_det_float(unitt_divide(a, b), (float)a / (float)b, 0.00001, "Random division failed")) return UNITT_E_FAIL;
        }
    }

    return UNITT_E_OK;
}

void unitt_main() 
{
    static UNITT_TCASE math_tests[] = {
        UNITT_TCASE(test_add),
        UNITT_TCASE(test_subtract),
        UNITT_TCASE(test_multiply),
        UNITT_TCASE(test_divide),
    };

    static UNITT suites[] = {
        { "Math Suite", math_tests, sizeof(math_tests) / sizeof(math_tests[0]) , unitt_clock },
    };

    UNITT_EXE(suites);
}

int period = 1000; // ms

static void test_base(void)
{
    srand((unsigned int)time(NULL));

#if defined(TEST_TARGET_unitt)
    while (1)
    {
        unitt_main();
        usleep(1000 * period);
    }
#else  
    printf("create task %d\r\n", task_create(period, unitt_main));
#endif
}

/************************************************************************************/
/*************************************  Command  ************************************/
/************************************************************************************/

static void usage(void)
{
    printf(
"Usage: unitt [opt] [arg] ...\n"
"\n"
"options:\n"
"    -p <period>         Test period, unit ms\n"
"    -h                  Print help\n"
"    -v                  Print version\n"
"\n"
    );
}

static int test(int argc, char *argv[])
{
    char *execute = NULL;
    int ut_period = 1000;

    /* reset getopt */
    command_opt_init();

    while (1)
    {
        int opt = command_getopt(argc, argv, "e:hvu::p:");
        if (opt == -1) break;

        switch (opt) 
        {
        case 'p' :
            period = atoi(command_optarg);
            break;
        case 'u' :
            if (command_optarg) ut_period = atoi(command_optarg);
            break;
        case 'e' :
            execute = command_optarg;
            break;
        case 'v' :
            printf("unitt version %d.%d.%d\r\n", UNITT_V_MAJOR, UNITT_V_MINOR, UNITT_V_PATCH);
            return 0;
        case '?':
            printf("Unknown option `%c`\r\n", command_optopt);
            return -1;
        case 'h' : 
        default:
            usage();
            return 0;
        }
    }

    if (execute)
    {
        if (!strcmp(execute, "base"))
        {
            test_base();
        }
        else if (!strcmp(execute, "ut"))
        {
            #if defined(TEST_TARGET_unitt)
            while (1)
            {
                unitt_task();
                usleep(1000 * ut_period);
            }
            #else  
            printf("create task %d\r\n", task_create(ut_period, unitt_task));
            #endif
        }
    }
    else  
    {
        test_base();
    }
    
    return 0;
}

/************************************************************************************/
/************************************ Test entry ************************************/
/************************************************************************************/

#if defined(TEST_TARGET_unitt)
int main(int argc, char *argv[])
{
    return test(argc, argv);
}
#else 
void test_unitt(void)
{
    command_export("unitt", test);
}
init_export_app(test_unitt);
#endif 
